package com.sumainfor.wx.system.gmio.service;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.wx.system.gmio.dao.TenantMapper;
import com.sumainfor.wx.system.gmio.dao.WaterEleGasMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Meng
 * @ClassName WaterEleGasMapper
 * @date 2019/8/13 13:49
 * @description: TODO 水电煤抄表
 */
@Service
public class WaterEleGasService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private WaterEleGasMapper waterEleGasMapper;

    public JsonResult meterReading(Map<String, Object> params) {
        if (!params.containsKey("meterReading") || !params.containsKey("HouId")) {
            return JsonResult.error("参数不正确");
        }
        //取出房源id1
        String houId = (String) params.get("HouId");
        //查询水电煤收费标准
        Map<String, Object> fees = getFees(houId);
        Double waterPricing = (Double) fees.get("WaterPricing");
        Double GasPricing = (Double) fees.get("GasPricing");
        Double EleFee = (Double) fees.get("EleFee");
        //获取sysId
        String userId = ContextUtils.getUserId();
        //判断该房源是否属于该服务管家
        Integer no = tenantMapper.getHouseNoByHouseIdAndSysId(houId, userId);
        if (no == 0 ) {
            return JsonResult.error("没有权限操作此房源");
        }
        //查询该房源是整租类型还是合租类型, 1合租, 2整租
        Integer houseTenantType = tenantMapper.getHouseTenantType(houId);
        if (houseTenantType == null) {
            return JsonResult.error("无相关租客信息");
        }
        if (houseTenantType != 1) {
            return JsonResult.error("服务管家只能录入合租抄表");
        }
        List<Map<String, Object>> tenantListByHouId = tenantMapper.getTenantListByHouId(houId);
        if (tenantListByHouId == null || tenantListByHouId.size() == 0) {
            return JsonResult.error("该房源下暂无租客信息");
        }
        // 电费抄表传1, 水费抄表传2, 煤气抄表传3
        Integer meterType = Integer.valueOf(params.get("meterType").toString());
        //电费抄表
        if (meterType == 1) {
            //查询本月电费是否抄过表
            int todayMonthEle = waterEleGasMapper.getEleBillCountOfTodayMonth(houId);
            if (todayMonthEle > 0 ) {
                return JsonResult.error("本月电表已抄过");
            }
        }
        //水费抄表
        if (meterType == 2) {
            int todayMonthWater = waterEleGasMapper.getWaterBillCountOfTodayMonth(houId);
            if (todayMonthWater > 0) {
                return JsonResult.error("本月水表已抄过");
            }
        }
        //燃气费抄表
        if (meterType == 3) {
            int todayMonthGas = waterEleGasMapper.getGasBillCountOfTodayMonth(houId);
            if (todayMonthGas > 0) {
                return JsonResult.error("本月燃气已抄表");
            }
        }

        return JsonResult.success();
    }

    public JsonResult eleMeterReading(String houId) {
        return null;
    }

    public JsonResult waterMeterReading(String houId, Double waterPrice,  Map<String, Object> params) {
        //查询该房源最近一次的抄表数
        Double lastWaterMeter = waterEleGasMapper.getLastWaterMeter(houId);
        //如果没抄过表, 按照0计算
        if (lastWaterMeter == null) {
            lastWaterMeter = 0.00;
        }
        //获取此次抄表数
        Double waterMeter = (Double) params.get("waterMeter");
        Map<String, Object> map = new HashMap<>();
        return null;
    }

    public JsonResult gasMeterReading(Map<String, Object> params) {
        return null;
    }

    /**
     * @Description //TODO 通过房源编号查询水电煤收费标准
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Param [houId]
     * @Date 2019/8/23 15:25
     **/
    public Map<String, Object> getFees(String houId) {
        //通过房源编号去合同表查询对应的收费中心编号
        String feesIdByHouId = waterEleGasMapper.getFeesIdByHouId(houId);
        if (feesIdByHouId.isEmpty()) {
            return null;
        }
        //通过收费中心编号去查询收费标准
        return waterEleGasMapper.getFees(feesIdByHouId);
    }
}
